<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
$_coreHelper = $this->helper('core');
$resource = Mage::getSingleton('core/resource');
$db = $resource->getConnection('core_read');
$storeId = Mage::app()->getStore()->getId();
$customerId = Mage::getSingleton("customer/session")->getCustomer()->getId();
$sql = "select sum(grand_total) from sales_flat_order where customer_id='{$customerId}' and status='complete'";

$query = $db->query($sql);
$total = $query->fetchColumn();

$sql = "select customer_id,sum(grand_total) as total from sales_flat_order where store_id='{$storeId}' and status='complete' group by customer_id order by total desc";
$query = $db->query($sql);
$items = $query->fetchAll();

$position = 1;
foreach($items as $item)
{
    if($item['total']>$total) break;
    $position++;
}
?>
<div class="dashboard">
    <div class="page-title">
        <h1><?php echo $this->__('商户中心') ?></h1>
    </div>
    <?php echo $this->getMessagesBlock()->toHtml() ?>
    <?php echo $this->getChildHtml('hello') ?>
    <div class="box-account box-info box-top">
		<div class="box-head">
			<h2>用户排行榜</h2>
		</div>
	    <div class="info-top">
	    	<p><strong>您当前的订单总额是: <span><?php echo $_coreHelper->formatPrice($total, false) ?></span></strong></p>
	    	<p><strong>您当前排名为: <span>第<?php echo $position;?>名</span></strong></p>
            <p>
                <strong>您的积分:
                    <span>
                    <?php 
                        $customer= Mage::getSingleton("customer/session")->getCustomer();
                        $account = Mage::helper('rewardpoints/customer')->getAccountByCustomer($customer);
                        if($account->getPointBalance())
                        {
                            echo $account->getPointBalance();
                        }else{
                            echo 0;
                        }
                     ?>
                    </span>
                </strong>
            </p>
	    </div>
	</div>
    <?php echo $this->getChildHtml('top') ?>

    <?php echo $this->getChildHtml('info1') ?>
    <?php echo $this->getChildHtml('info2') ?>
</div>




